
library(data.table)
library(tidyverse)
library(tm)
library(tidygraph)
library(jsonlite)
library(skimr)
library(tidygraph)
melt = reshape2::melt
library(broom)
dcast = reshape2::dcast
library(stringr)
library(Matrix)
library(xtable)
library(intergraph)

#######################################################################################
######################## Plotting begins here #########################################
#######################################################################################


## Load the imputed ideal points from above
load("data/acnet_scores_5_13_22.RData")

acnet_final = acnet_final %>% select(orgID, exogenous, orgname, acnet_score, DIME, brief)

sum(is.na(acnet_final$acnet_score)) # 289


## I want one plot that shows the distribution
hist(acnet_final$acnet_score[acnet_final$acnet_score != 0], breaks=15,
     main = "Distribution of ACNet Scores", 
     xlab = "ACNet Score")

# I want another plot to place key groups alongside key congressional leaders
# Since this is all on the Bonica spectrum
cong = read.csv("data/dime_recipients_1979_2014.csv") # just for reference
# Let's find pelosi, mcconnell, sanders, susan collins, kevin mccarthy, 
cong2 = cong[cong$name %in% c("pelosi, nancy", "mcconnell, mitch", "sanders, bernard",
                              "collins, susan m", "rohrabacher, dana", "ryan, paul d",
                              "cruz, rafael edward ted", "harris, kamala d",
                              "obama, barack", "clinton, hillary rodham", "paul, ron",
                              "sessions, jeff", "cheney, richard bruce", "schumer, charles e",
                              "manchin iii, joe"),
             c("name", "recipient.cfscore")]
cong2 = cong2[!duplicated(cong2$name),]
cong2$is.train = 1
colnames(cong2)[1:2] = c("org", "ideal")

temp = acnet_final[order(acnet_final$acnet_score),]
temp = temp[temp$exogenous == TRUE & temp$brief == FALSE,]
temp = temp[!is.na(temp$orgname),]
head(temp[,c("orgname", "acnet_score")], 100)
# >50 amici
# inspect...

## These ids are for when I use my rather than Brian's data frame
#ids = c(4767, 8013, 8027, 8364, 8981, 591, 1068, 13810, 1039, 1493, 882,5525, 11483 , 7103, 9409,712,
# 8339, 5753, 6769, 5593, 12722, 8869, 12598, 4223, 7039, 12386, 6434, 12236,3567,5408, 5015, 3567, 11314, 8435,
# 2982, 11990, 4654, 1478, 8271, 2553, 3006)

toview = temp[,c( "orgID","orgname", "acnet_score", "exogenous")]

acnet_final[grepl("clif", acnet_final$orgname),]

ids = c(2468, 1295, 1041, 1914, 2612, 204, 260, 279, 351, 509, 573, 760, 910, 11048, 3299,9779,
        3926, 13348, 5008, 3405, 8626, 2105, 153, 3392, 4267, 1655, 1821, 11269, 3503, 2093, 2696,
        16180, 1595, 1143, 10722, 3726, 1577, 2708, 14355, 1770, 2022, 2692,16847,2686,10342)

#plotdf = out[ids,1:3] 
plotdf = acnet_final[acnet_final$orgID %in% ids, c("orgname", "acnet_score", "exogenous")]
plotdf = plotdf %>% arrange(acnet_score)
colnames(plotdf) = c("org", "ideal", "is.train")
plotdf$is_org = TRUE

cong2$is_org = FALSE

plotdf = rbind(plotdf, cong2)
plotdf = plotdf[order(plotdf$ideal),]
plotdf$org = toupper(plotdf$org)

rbPal <- colorRampPalette(c('dodgerblue4', 'firebrick4'))
plotdf$Col <- rbPal(10)[as.numeric(cut(plotdf$ideal,breaks = 10))]
plotdf$id = 1:nrow(plotdf)
plotdf$even = (plotdf$id %% 2 == 0)
plotdf$nc = nchar(as.character(plotdf$org))
plotdf$labelloc = NA
plotdf$labelloc[plotdf$even == FALSE] = plotdf$ideal[plotdf$even == FALSE] - plotdf$nc[plotdf$even == FALSE]/77
plotdf$labelloc[plotdf$even == TRUE] = plotdf$ideal[plotdf$even == TRUE] +.1 + plotdf$nc[plotdf$even == TRUE]/22

plotdf$pch = 17
plotdf$pch[plotdf$is_org==1 & plotdf$is.train == 1] = 19
plotdf$pch[plotdf$is_org==1 & plotdf$is.train == 0] = 15


png(filename="figures/fig2_v6.png", width=5000, height=4000)

par(mar=c(6.1,6.1,2.1,4.1))
plot(x = plotdf$ideal, y = 1:nrow(plotdf), pch=plotdf$pch, cex=4, xlab = "", yaxt='n', bty="n",
     ylab=NA, cex.lab=4, col=plotdf$Col, xlim = c(-3.2,3.2), cex.axis=4, xaxt='n')
#axis(1, at = c(-3:3), labels = -3:3, cex.axis = 4)
segments(x0=0, x1=0, y0=.25, y1=nrow(plotdf), col="darkgrey", lty="dashed", lwd=3)
text(x=plotdf$labelloc, y=1:nrow(plotdf), labels=plotdf$org, cex=4, pos = 2, col=plotdf$Col)
abline(h=.25, col="black", lwd=3)
segments(x0=-3:3, x1=-3:3, y0=.25, y=-.25, lwd=3)
text(x=-3:3, y=-.65, labels=-3:3, cex=4)
#text(x=-.2, y=26.5, col="blue", labels="More Liberal")
#text(x=.275, y=26.5, col="darkred", labels="More Conservative")
dev.off()


######################################## THe same as above, for briefs
## Load the imputed ideal points from above
load("data/acnet_scores_5_13_22.RData")

## Merge with the vertex data
acnet_final = acnet_final %>% select(orgID, exogenous, orgname, acnet_score, DIME, brief)


full.data.briefs = acnet_final %>% filter(brief==TRUE)


library(tidyverse)
library(tm)
library(jsonlite)

briefs = read.csv("data/acnet_Briefs.csv")

out = merge(full.data.briefs, briefs, by.x="orgID", by.y="briefs")

cases_to_include = data.frame(caseName = 
  c("TIMES-PICAYUNE PUBLISHING CO. ET AL. v. UNITED STATES",
                     "REGENTS OF THE UNIVERSITY OF CALIFORNIA v. BAKKE",
                     "KOREMATSU v. UNITED STATES",
                     "SHELBY COUNTY v. HOLDER",
                     "CITIZENS UNITED v. FEDERAL ELECTION COMMISSION",
                     "AT&T MOBILITY LLC PETITIONER v. VINCENT CONCEPTION ET UX",
                     "NATIONAL FEDERATION OF INDEPENDENT BUSINESS et al. PETITIONERS v. KATHLEEN SEBELIUS SECRETARY OF HEALTH AND HUMAN SERVICES et al.",
                     "ROE et al. v. WADE DISTRICT ATTORNEY OF DALLAS COUNTY",
                     "MIRANDA v. ARIZONA",
                     "BROWN et al. v. BOARD OF EDUCATION OF TOPEKA et al.",
                     "ENGEL et al. v. VITALE et al.",
                     "LOVING et ux. v. VIRGINIA",
                     "TERRY v. OHIO",
                     "PHILLIPS v. MARTIN MARIETTA CORP.",
                     "WISCONSIN v. YODER et al",
                     "SAN ANTONIO INDEPENDENT SCHOOL DISTRICT et al. v. RODRIGUEZ et al.",
                     "UNITED STATES v. NIXON PRESIDENT OF THE UNITED STATES et al.",
                     "BUCKLEY et al. v. VALEO SECRETARY OF THE UNITED STATES SENATE et al.",
                     "CHEVRON U. S. A. INC. v. NATURAL RESOURCES DEFENSE COUNCIL INC. et al.",
                     "ABIGAIL NOEL FISHER PETITIONER v. UNIVERSITY OF TEXAS AT AUSTIN et al.",
                     "TEXAS v. JOHNSON",
                     "JOHN GEDDES LAWRENCE AND TYRON GARNER v. TEXAS",
                     "DISTRICT OF COLUMBIA v. HELLER"),
  shortnames = c("Times-Picayune v US", "Bakke v University of California", "Korematsu", "Shelby v Holders",
               "Citizens United", "AT&T v Conception", "NFIB v Sebelius",
               "Roe v Wade", "Miranda v Arizona", "Brown v Board",
               "Engel v Vitale", "Loving v Virginia",
               "Terry v Ohio", "Phillips v Marietta", "Yoder",
               "San Antonio v Rodriguez", "US v Nixon",
               "Buckley v Valeo", "Chevron v NRDC", "Fisher v University of Texas",
               "Texas v Johnson", "Lawrence v Texas", "Heller v District of Columbia"),
  libwinner = c(0 , 0,0,0,0,0,1,1,1,1,1,1,0,1,1,0,1,1,1,1,1,1,0)
  )
cases_to_include = cases_to_include %>%
  mutate(winnercolor = ifelse(libwinner, 'dodgerblue4', 'firebrick4'))

plotdf = merge(out, cases_to_include, by="caseName")
plotdf = plotdf %>% filter(!is.na(acnet_score)) %>%
  select(acnet_score, shortnames, libwinner) 

plotdf = plotdf%>% group_by(shortnames) %>%
  summarize(minbrief = min(acnet_score),
            meanbrief = mean(acnet_score)) %>%
  arrange(meanbrief) %>% mutate(y = 1:nrow(.)) %>%
  right_join(plotdf) %>% arrange(acnet_score)

rbPal <- colorRampPalette(c('dodgerblue4', 'firebrick4'))
plotdf$Col <- rbPal(10)[as.numeric(cut(plotdf$acnet_score,breaks = 10))]
plotdf$Col2 <- ifelse(plotdf$libwinner==1, "dodgerblue4", "firebrick4")

png(filename="figures/fig5_v2.png", width=2500, height=2000)
par(mar=c(6.1,6.1,2.1,4.1))
plot(x = plotdf$acnet_score, y = plotdf$y, pch=16, cex=4, xlab = "", yaxt='n', bty="n",
     ylab=NA, cex.lab=4, col=plotdf$Col, xlim = c(-3.2,3.2), cex.axis=4, xaxt='n')
#axis(1, at = c(-3:3), labels = -3:3, cex.axis = 4)
segments(x0=0, x1=0, y0=.75, y1=nrow(plotdf), col="darkgrey", lty="dashed", lwd=3)
text(x=plotdf$meanbrief, y=plotdf$y+0.4, labels=plotdf$shortnames, cex=4, pos = 2, col=plotdf$Col2)
abline(h=.75, col="black", lwd=3)
#segments(x0=-3:3, x1=-3:3, y0=.25, y=-.25, lwd=3)
text(x=-3:3, y=0.5, labels=-3:3, cex=4)
#text(x=-.2, y=26.5, col="blue", labels="More Liberal")
#text(x=.275, y=26.5, col="darkred", labels="More Conservative")
dev.off()



######################################### Plot the Full network

## Load the imputed ideal points from above
load("data/acnet_scores_5_13_22.RData")

source('code/estimation_functions.R')

m = make_orgs_only_graph()

library(GGally)
library(statnet)
library(ggnetwork)
library(RColorBrewer)
library(igraph)
library(dplyr)
library(stringr)



idx = V(m)$name %in% acnet_final$orgname
m = delete_vertices(m, !idx)

out = acnet_final[acnet_final$orgname %in% V(m)$name,]
out = out[match(V(m)$name, out$orgname),]

out$acnet_score[is.na(out$acnet_score)] = 0

net = m

gdata::keep(net, out, sure=T)

net2 = igraph::simplify(net)


out$id = 1:nrow(out)
colfun <-  colorRampPalette(c("dodgerblue4", "firebrick4"))
out = out[order(out$acnet_score, decreasing=F),]
colfun <-  colorRampPalette(c("dodgerblue4", "firebrick4"))
out$colors = colfun(nrow(out))
out = out[order(out$id, decreasing=F),]

plot_df <- data.frame(
  id = V(net2)$name,
  degree = igraph::degree(net2),
  ideal = out$acnet_score,
  is.train = out$exogenous,
  # ndocs = out$ndocs,
  colors = out$colors,
  orgid = out$orgID)

#mean(plot_df$ideal, na.rm=T)
#weighted.mean(plot_df$ideal, plot_df$degree, na.rm=T)

plot_df$shape <- 19
plot_df$shape[plot_df$is.train == FALSE] = 15

# Make the NAs grey
plot_df$colors = as.character(plot_df$colors)
plot_df$colors[plot_df$ideal==0] = "grey25"

plot_df$name_id = paste(plot_df$id, plot_df$orgid, sep = "--")

set_vertex_attr(net2, 'color', value=as.character(plot_df$colors))
cdknet <- ggnet2(net2, node.color = as.character(plot_df$colors),
                 size = "degree", node.shape = plot_df$shape,
                 node.alpha=0.4,label=F, label.size=2,
                 label.alpha=0.7, edge.alpha=0.1) + guides(size = F)
ggsave(cdknet, file = "figures/fig3_v5.pdf", width=7, height=7, units="in")



# Make ego nets
make_amicus_ego = function(node, nodename, trim_labels=F, orgs_to_label = NULL){
  set.seed(12345)
  aclu = make_ego_graph(net, order=1, nodes=node)
  V(aclu[[1]])$ideal = out$acnet_score[out$orgname %in% vertex_attr(aclu[[1]])$name]
  #net_aclu = asNetwork(aclu[[1]])
  plot_df <- data.frame(
    id = V(aclu[[1]])$name,
    degree = igraph::degree(aclu[[1]]))
  plot_df <- merge(out, plot_df, by.x="orgname", by.y="id")
  plot_df <- plot_df[order(match(plot_df$orgname, V(aclu[[1]])$name)),]
  plot_df$shape <- 19
  plot_df$shape[plot_df$training == FALSE] = 15
  plot_df$shape[plot_df$id == node] = 18
  #plot_df$organizationName = toupper(plot_df$organizationName)
  net_aclu = aclu[[1]]
  net_aclu = set_vertex_attr(net_aclu, name='color', value=plot_df$colors)
  net_aclu = set_vertex_attr(net_aclu, name='shape', value=plot_df$shape)
  if(trim_labels==TRUE){
    cdknet <- ggnet2(igraph::simplify(net_aclu), node.color = "color", size = "degree", node.shape = plot_df$shape, node.alpha=0.65,
                     label=plot_df$orgname[plot_df$degree > 100], label.size=2, label.alpha=0.5, edge.alpha=0.2) + guides(size = F) #+ 
    #  geom_nodelabel_repel(aes(label=plot_df$id[plot_df$degree > 100]), label.size=2, label.alpha=0.5)
  } else if(!is.null(orgs_to_label)){
    cdknet <- ggnet2(igraph::simplify(net_aclu), node.color = "color", size = "degree", node.shape = plot_df$shape, node.alpha=0.65,
                     label=plot_df$orgname[plot_df$orgname %in% orgs_to_label], label.size=7, label.alpha=0.5, edge.alpha=0.2) + guides(size = F) #+ 
  }else if(trim_labels == "full"){
    cdknet <- ggnet2(igraph::simplify(net_aclu), node.color = "color", size = "degree", node.shape = plot_df$shape, node.alpha=0.65,
                     label=plot_df$orgname, label.size=2, label.alpha=0.5, edge.alpha=0.2) + guides(size = F) #+ 
  } else{
    cdknet <- ggnet2(igraph::simplify(net_aclu), node.color = "color", size = "degree", node.shape = plot_df$shape,node.alpha=0.65,
                     label=FALSE, label.size=2, label.alpha=0.5, edge.alpha=0.2) + guides(size = F) #+ 
    # geom_nodelabel_repel(aes(label=plot_df$id), label.size=2, label.alpha=0.5)
  }
  filename = paste0("figures/ego_", nodename, ".png") 
  ggsave(cdknet, file = filename, width=7, height=7, units="in")
  out = mean(unlist(plot_df$acnet_score))
  return(out)
}


## make the orgs from fig1_v2
## These go in the paper!

a = which(plot_df$id=="feminist majority foundation")
b = which(plot_df$id=="naral pro-choice america foundation")
a1 = make_amicus_ego(node=a, nodename = "feministmajority", trim_labels = FALSE) # -.650
b1 = make_amicus_ego(node=b, nodename = "naral", trim_labels = FALSE) # -.787
a2 = out$acnet_score[out$orgname == "feminist majority foundation"] # -0.632
b2 = out$acnet_score[out$orgname == "naral pro-choice america foundation"] # -0.863
# Slide plots
#make_amicus_ego(node=a, nodename = "feministmajority_2", trim_labels = FALSE,
#                orgs_to_label = c("rock the vote", "national congress of black women", "republicans for choice", "american nurses association"))
#make_amicus_ego(node=b, nodename = "naral_2", trim_labels = FALSE, 
#                orgs_to_label = c("naral pro choice arizona", "naral pro-choice texas", "women's law project", "women employed"))


a=which(plot_df$id=="new york times co")
b=which(plot_df$id=="johnson and johnson co")
c1 = make_amicus_ego(node=a, nodename = "NYT", trim_labels = FALSE) # 0.155
d1 = make_amicus_ego(node=b, nodename = "johnsonandjohnson", trim_labels = FALSE) # 0.150
c2 = out$acnet_score[out$orgname == "new york times co"] # 0.197
d2 = out$acnet_score[out$orgname == "johnson and johnson co"] # 0.141
# Slide plots
#make_amicus_ego(node=a, nodename = "NYT_2", trim_labels = FALSE,
#                orgs_to_label = c("time warner inc", "walmart stores inc", "los angeles times co", "bloomberg lp", "comcast corp"))
#make_amicus_ego(node=b, nodename = "johnsonandjohnson_2", trim_labels = FALSE, 
#                orgs_to_label = c("chevron corp", "eli lilly and co", "dow chemical co", "coca-cola co", "bristol-myers squibb co", "kpmg llp"))


a=which(plot_df$id=="free speech defense and education fund inc")
b=which(plot_df$id=="gun owners of america inc")
e1 = make_amicus_ego(node=a, nodename = "freespeechdefense", trim_labels = FALSE) # 0.974
f1 = make_amicus_ego(node=b, nodename = "gunownersofamerica", trim_labels = FALSE) # 0.731
e2 = out$acnet_score[out$orgname == "free speech defense and education fund inc"] # 1.048
f2 = out$acnet_score[out$orgname == "gun owners of america inc"] # 0.919
# Slide plots
#make_amicus_ego(node=a, nodename = "freespeechdefense_2", trim_labels = FALSE,
#                orgs_to_label = c("capitol hill prayer alert foundation", "gun owners of america inc", "citizens united", "60 plus association inc")) # 0.974
#make_amicus_ego(node=b, nodename = "gunownersofamerica_2", trim_labels = FALSE,
#                orgs_to_label = c( "citizens united", "60 plus association inc", "rutherford institute",  "electronic frontier foundation"))
                


# Overall avgs
g1 = mean(out$acnet_score) # -0.15
g2 = weighted.mean(plot_df$ideal, w=plot_df$degree)

vec = c(f1,f2,e1,e2,d1,d2,c1,c2,b1,b2,a1,a2,g1,g2)
vec = vec[!is.na(vec)]

tab3 = matrix(vec,
              ncol=2,
              byrow=TRUE)
rownames(tab3) = c("Gun Owners of America",
                   "Free Speech Defense and Education Fund",
                   "New York Times",
                   "Johnson & Johnson",
                   "Feminist Majority Foundation",
                   "NARAL Pro-Choice America",
                   "Overall Average")
colnames(tab3) = c("Org Ideology", "Ego Network Ideology")
print(xtable::xtable(tab3, auto=T, digits=2),
      include.rowanmes=F,
      booktabs=T, floating=F,
      file="tables/appendix_table_1.tex",
      sanitize.text.function=function(x){x},
      format.args=list(big.mark=","))


## Fig 7
#### trying a full scatter 
# ideology is a org-by-case level data set (but I actually want brief-level)
source("code/acnet.R")

load("data/acnet_scores_5_13_22.RData")
acnet_final = acnet_final %>% select(orgID, exogenous, orgname, acnet_score, DIME, brief)
full.data.briefs = acnet_final %>% filter(brief==TRUE)
briefs = read.csv("data/acnet_Briefs.csv")
out = merge(full.data.briefs, briefs, by.x="orgID", by.y="briefs")

out$opinionYear = as.numeric(substr(out$caseId, 1,4))

scatter_gam <- ggplot(out, aes(x=opinionYear,
                                    y=acnet_score,
                                    color=acnet_score)) +
  geom_jitter(alpha=0.05, height=0.05, width= 0.25,
             # color="#c0395b"
  ) +
  scale_color_gradient(low="darkblue", high="darkred",
                       name="Ideology", limits = c(-2,2)) + 
  # scale_x_continuous(breaks=seq(1917,2012, by=1)) +
  xlim(1940,2020) +
  # scale_y_discrete(breaks=seq(-1,1, by=.5)) +
  ylim(-2,2) + 
  geom_smooth(alpha=0.25, color="white", fill="black") +
  labs(x="Decision Year", y="Amicus Brief IGNet Score", title="") +
  theme_bw()
scatter_gam

ggsave(scatter_gam, filename = "figures/fig6.png",
       width=8, height=6,units = 'in')



######################################################################################################
#Figure 4 (in paper) - density plot by ideology methods --- donors - nonDonors
######################################################################################################
load("data/acnet_scores_5_13_22.RData")
table(acnet_final$brief, is.na(acnet_final$acnet_score))

acnet_final = acnet_final %>% filter(brief == FALSE)
table(acnet_final$exogenous, is.na(acnet_final$acnet_score))

acnet_final$Organization = ifelse(acnet_final$exogenous==TRUE,
                                "Donor", "Non-Donor")
names(acnet_final)[which(names(acnet_final) == "acnet_score")] <- "scores"
library(ggplot2)

mu <- plyr::ddply(acnet_final, "Organization", dplyr::summarise, grp.mean=mean(scores, na.rm=T))

p <- ggplot(acnet_final %>% filter(!is.na(scores)),
            aes(x=scores, color=Organization)) +
  geom_density(aes(fill=Organization), alpha=.75) +
  scale_fill_manual(values=c("forestgreen", "mediumorchid1")) + 
  geom_segment(data = mu, aes(x=grp.mean, xend=grp.mean, y=0, yend=2.6,
                              color=Organization),
               linetype="dashed", lwd=1) +
  theme_bw() + xlab("Organization Ideology") + ylab("") +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.border = element_blank(), axis.text.y = element_blank(), axis.ticks.y = element_blank(), 
        legend.position = c(0.9, 0.45)) +
  xlim(c(-2.5, 2.5)) +
  scale_color_manual(values = c("lightgrey", "lightgrey"))

p
ggsave(p,filename = "figures/fig4.pdf")



######################################################################################################
#Tables 4 (in paper) - donors vs nondonors summary statistics 
######################################################################################################

acnet_final %>% filter(!is.na(scores)) %>%
  group_by(Organization) %>%
  summarize(`Avg. Score` =mean(scores, na.rm=T), 
            SD = sd(scores, na.rm=T),
            `No. Orgs` = n())

# A tibble: 2 x 4
#Organization `Avg. Score`    SD `No. Orgs`
#<chr>               <dbl> <dbl>      <int>
#  1 Donor             -0.0475 0.597       2973
#2 Non-Donor         -0.217  0.571       9576


######################################################################################################
#In-text figures
######################################################################################################
tmp = acnet_final %>% filter(exogenous == FALSE & !is.na(scores) == FALSE)
ids = tmp$orgID

load("data/amicus_adjacency_matrix.RData")

#1e

sum(rowSums(m[ids,]) == 0)

#1f

2845 - sum(rowSums(m[ids,]) == 0)
